package xhwl.insurance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import xhwl.insurance.R;
import xhwl.insurance.http.bean.resp.CardDetailResq;

/**
 * Created by chengang on 2017/7/28
 */

public class CardDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<CardDetailResq> mlistVo;
    public CardDetailsAdapter(Context mContext, ArrayList<CardDetailResq> mlistVo){
        this.mContext = mContext;
        this.mlistVo = mlistVo;
    }

    @Override
    public int getCount() {
        return mlistVo.size();
    }

    @Override
    public Object getItem(int position) {
        return mlistVo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
          ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.card_detail_item, null);
            holder = new ViewHolder();
            holder.num_tv = (TextView) convertView.findViewById(R.id.num_tv);
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.iphnoe_tv = (TextView) convertView.findViewById(R.id.iphnoe_tv);
            holder.join_time = (TextView) convertView.findViewById(R.id.join_time);
            holder.take_effect_time = (TextView) convertView.findViewById(R.id.take_effect_time);
            holder.invalid_time = (TextView) convertView.findViewById(R.id.invalid_time);
            holder.title_rly = (RelativeLayout) convertView.findViewById(R.id.title_rly);
            holder.right_view = (ImageView) convertView.findViewById(R.id.right_view);
            holder.time_ly = (LinearLayout) convertView.findViewById(R.id.time_ly);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name_tv.setText(mlistVo.get(position).getPERSON_NAME());
        holder.iphnoe_tv.setText(getIphone(mlistVo.get(position).getPERSON_MOBILE().toString().trim()));
        String jtime[] = mlistVo.get(position).getBUY_DATE().split(" ");
        holder.join_time.setText(jtime[0]);
        String sTime[] = mlistVo.get(position).getSTART_DATE().split(" ");
        holder.take_effect_time.setText(sTime[0]);
        String eTime[] = mlistVo.get(position).getEND_DATE().split(" ");
        holder.invalid_time.setText(eTime[0]);
        holder.num_tv.setText("(" + mlistVo.get(position).getCARD_COUNT() + "张" + ") " );
        holder.title_rly.setOnClickListener(new OnMyClickListener(holder,position));
        return convertView;
    }


    public class OnMyClickListener implements View.OnClickListener{
        private ViewHolder holder;
        private int position;
        public OnMyClickListener(ViewHolder holder,int position){
            this.position = position;
            this.holder = holder;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.title_rly:
                    if (holder.time_ly.getVisibility() == View.VISIBLE){
                        holder.time_ly.setVisibility(View.GONE);
                        holder.right_view.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tubiaoright));
                    }else {
                        holder.time_ly.setVisibility(View.VISIBLE);
                        holder.right_view.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tubiaodown));
                    }
                    break;
                default:
                    break;
            }
        }
    }
    class ViewHolder {
        TextView name_tv;
        TextView num_tv;
        TextView iphnoe_tv;
        TextView join_time;
        TextView take_effect_time;
        TextView invalid_time;
        RelativeLayout title_rly;
        ImageView right_view;
        LinearLayout time_ly;

    }

    private String getIphone(String pNumber) {
        String phone = "";
        if (!"".equals(pNumber) && pNumber.length() > 6){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pNumber.length(); i++) {
                char c = pNumber.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            phone = sb.toString().trim();
        }

        return phone;
    }
}
